package com.zxw.hotel.api;

import com.github.pagehelper.PageInfo;
import com.zxw.hotel.common.Pageinfo;
import com.zxw.hotel.model.Admin;
import com.zxw.hotel.model.Room;
import com.zxw.hotel.model.extend.AdminExtend;
import com.zxw.hotel.model.extend.RoomExtend;
import com.zxw.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/room")

public class RoomApi {
    private RoomService roomService;
    @Autowired
    public RoomApi(RoomService roomService) {
        this.roomService = roomService;
    }

//查询  可以通过  facility的名字查询
    @GetMapping
    public Map<String, Object> getall(@RequestParam(defaultValue = "1") Integer pageNo,
                                      @RequestParam(defaultValue = "2147483647") Integer pageSize,
                                      RoomExtend roomExtend) {
        if (pageNo == null || pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = Integer.MAX_VALUE;
        }

        Pageinfo pageinfo = new Pageinfo(pageNo, pageSize);
        List<Room> allroom = roomService.getAll(pageinfo, roomExtend);
        //通过分页插件获取分页信息
        PageInfo<Room> pi = new PageInfo<>(allroom);
        Map<String, Object> map = Map.of("total", pi.getTotal(), "pageNo", pi.getPageNum(), "pageSize", pi.getPageSize());
        return Map.of("success", true, "data", allroom, "pi", map);
    }
//删除 根据id 数组删除
    @DeleteMapping
    public ResponseEntity<Map<String, Object>> del(@RequestBody Integer[] ids) {
        int delete = roomService.delete(ids);
        return ResponseEntity.ok().body(Map.of("success", delete>0));
    }
    //新增 指定facilityName
    @PostMapping
    public ResponseEntity<Map<String, Object>> add(@RequestBody RoomExtend roomExtend) {
        int add = roomService.add(roomExtend);
        return ResponseEntity.ok().body(Map.of("success", add>0));
    }
    //修改 指定facilityName
    @PutMapping
    public ResponseEntity<Map<String, Object>> update(@RequestBody RoomExtend roomExtend) {
        int update = roomService.update(roomExtend);
        return ResponseEntity.ok().body(Map.of("success", update>0));
    }

}
